package com.breeze.leetcode.easy;

import java.util.ArrayList;
import java.util.List;

/**
 * @url <a href="https://leetcode.cn/problems/design-an-ordered-stream/">1656. 设计有序流</a>
 * @author yangzh
 * @date 2025/2/24
 */
public class T1656OrderedStream {
    private final String[] orders;
    private Integer ptr = 1;

    public T1656OrderedStream(int n) {
        orders = new String[n];
    }

    public List<String> insert(int idKey, String value) {


        orders[idKey - 1] = value;
        List<String> res = new ArrayList<>();
        if (ptr == idKey) {
            for (int i = idKey - 1; i < orders.length; i++) {
                if (orders[i] != null) {
                    res.add(orders[i]);
                    ptr++;
                } else {
                    break;
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        T1656OrderedStream os= new T1656OrderedStream(5);
        System.out.println(os.insert(3, "ccccc"));
        System.out.println(os.insert(1, "aaaaa"));
        System.out.println(os.insert(2, "bbbbb")); // 插入 (2, "bbbbb")，返回 ["bbbbb", "ccccc"]
        System.out.println(os.insert(5, "eeeee")); // 插入 (5, "eeeee")，返回 []
        System.out.println(os.insert(4, "ddddd"));
    }
}
